<?php

	set_error_handler( 'HandlerEngine::errorHandler' );
	set_exception_handler( 'HandlerEngine::exceptionHandler' );
	
	class HandlerEngine
	{	
		private static function printErrMsg( $exception )
		{
            $file = fopen( "includes/error.inc.txt", "w" );

            fwrite( $file, 'ERROR: '. $exception->getMessage() .  ". Error code: " . $exception->getCode()."\n" ); 

            fclose( $file );
		}
		
		public static function exceptionHandler( $exception ) 
		{	
			self::printErrMsg( $exception );
			
			header ("Location: error.php");
		}

		public static function errorHandler( $no, $str, $file, $line ) 
		{
			$exception = new Exception( $str . " in " . $file . " on line " . $line, $no );
			self::exceptionHandler( $exception );
		}
	}

?>